﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Data;
using Microsoft.SharePoint.Utilities;

namespace SPMigrationOracleUtility
{
    class ClsLog
    {
        FieldUrlValue itemUrl;
        string SiteUrl = Convert.ToString(SPMigrationOracleUtility.Appl.Default.SiteURL);
        DataTable dt = new DataTable();
        DataRow dr;

        /// <summary>
        /// Constructor to create DataTable
        /// </summary>
        public ClsLog()
        {
            GetDataTable();
        }

        /// <summary>
        /// Get all records from 'Migration Log' List 
        /// </summary>
        /// <returns>Return all records as DataTable</returns>
        public DataTable GetAllRecords(string migMode)
        {
            try
            {
                using (ClientContext clientContext = new ClientContext(SiteUrl))
                {
                    Web oweb = clientContext.Web;
                    List olist = clientContext.Web.Lists.GetByTitle("Migration Log");
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='MappingMode' /><Value Type='Choice'>" + migMode + "</Value></Eq></Where></Query></View>";
                    ListItemCollection olistcoll = olist.GetItems(camlQuery);
                    clientContext.Load(olist);
                    clientContext.ExecuteQuery();
                    clientContext.Load(olistcoll);
                    clientContext.ExecuteQuery();
                    foreach (ListItem oitem in olistcoll)
                    {
                        dr = dt.NewRow();
                        dr["Src Table"] = Convert.ToString(oitem["Title"]);
                        dr["Destination List"] = Convert.ToString(oitem["DestList"]);
                        dr["Src Table Fld"] = Convert.ToString(oitem["SrcTableField"]);
                        dr["Destination List Fld"] = Convert.ToString(oitem["DestListField"]);
                        itemUrl = (FieldUrlValue)oitem["ItemUrl"];
                        if(itemUrl !=null)
                            dr["ItemUrl"] = itemUrl.Url;
                        dr["Created Date"] = Convert.ToString(oitem["Created"]);
                        dr["Status"] = Convert.ToString(oitem["Status"]);
                        string cmts = Convert.ToString(oitem["Comments"]);
                        dr["Comments"] = HtmlRemoval.StripTagsRegex(cmts);
                        dt.Rows.Add(dr);   
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid URL" + ex);
            }
            return dt;
        }

        /// <summary>
        /// Filter 'Migration Log' List records based on Created field.
        /// </summary>
        /// <param name="frmDate">Filter Begins Date</param>
        /// <param name="toDate">Filter Ends Date</param>
        /// <returns>Return the DataTable with the filtered Records</returns>
        public DataTable GetFilterRecords(DateTimePicker frmDate, DateTimePicker toDate, string migMode)
        {
            try
            {
                using (ClientContext clientContext = new ClientContext(SiteUrl))
                {
                    
                    Web oweb = clientContext.Web;
                    List olist = clientContext.Web.Lists.GetByTitle("Migration Log");
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='MappingMode' /><Value Type='Choice'>" + migMode + "</Value></Eq><And><Geq><FieldRef Name='Created' /><Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(frmDate.Value) +"</Value></Geq><Leq><FieldRef Name='Created' /><Value IncludeTimeValue='FALSE' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(toDate.Value) +"</Value></Leq></And></And></Where></Query></View>";
                    ListItemCollection olistcoll = olist.GetItems(camlQuery);
                    clientContext.Load(olist);
                    clientContext.ExecuteQuery();
                    clientContext.Load(olistcoll);
                    clientContext.ExecuteQuery();
                    foreach (ListItem oitem in olistcoll)
                    {
                        dr = dt.NewRow();
                        dr["Src Table"] = Convert.ToString(oitem["Title"]);
                        dr["Destination List"] = Convert.ToString(oitem["DestList"]);
                        dr["Src Table Fld"] = Convert.ToString(oitem["SrcTableField"]);
                        dr["Destination List Fld"] = Convert.ToString(oitem["DestListField"]);
                        itemUrl = (FieldUrlValue)oitem["ItemUrl"];
                        if(itemUrl != null)
                        dr["ItemUrl"] = itemUrl.Url;
                        dr["Created Date"] = Convert.ToString(oitem["Created"]);
                        dr["Status"] = Convert.ToString(oitem["Status"]);
                        string cmts =  Convert.ToString(oitem["Comments"]);
                        dr["Comments"] = HtmlRemoval.StripTagsRegex(cmts);
                        dt.Rows.Add(dr);
                        oitem.Update();
                        clientContext.ExecuteQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid URL" + ex);
            }
            return dt;

        }

        /// <summary>
        /// Create DataTable to store records of Migration Log List
        /// </summary>
        public void GetDataTable()
        {
            dt.Columns.Add("Src Table", typeof(string));
            dt.Columns.Add("Destination List", typeof(string));
            dt.Columns.Add("Src Table Fld", typeof(string));
            dt.Columns.Add("Destination List Fld", typeof(string));
            dt.Columns.Add("ItemUrl", typeof(string));
            dt.Columns.Add("Created Date", typeof(string));
            dt.Columns.Add("Status", typeof(string));
            dt.Columns.Add("Comments", typeof(string));
        }

    }
}
